<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Controller;

use App\Constants\ErrorCode;
use App\Constants\ServerCode;
use App\ENUM\ENUM_PAGE;
use App\Middleware\UserMiddleware;
use App\Service\ReturnMessageService;
use Hyperf\Di\Annotation\Inject;
use App\Service\ArticleService;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Request;

#[Controller(prefix: "/article")]
#[Middlewares([UserMiddleware::class])]
class ArticleController extends AbstractController
{

    #[inject]
    protected ArticleService $article_service;

    #[inject]
    protected ReturnMessageService $returnMessageService;


    #[GetMapping(path: 'index')]
    public function index()
    {
        return [
            'title' => '文章标题',
            'content' => '文章内容',
        ];
    }

    #[GetMapping(path: 'column/msg')]
    public function getColumn(Request $request)
    {
        $column_id = $request->input('id');

        if (empty($column_id)){
            return $this->returnMessageService->buildReturnMessage(false,"栏目id不能为空",ServerCode::ERROR_PARAMETERS);
        }

        $column_list = $this->article_service->getColumnMessage($column_id);

        return $this->returnMessageService->buildReturnMessage(true, $column_list, ServerCode::SUCCESS_CODE);
    }

    #[GetMapping(path: 'column')]
    public function getArticleColumnList()
    {
        $column_list = $this->article_service->getArticleColumn();

        return $this->returnMessageService->buildReturnMessage(true, $column_list, ServerCode::SUCCESS_CODE);
    }

    #[GetMapping(path: "list")]
    public function getArticleList(Request $request)
    {
        $column_id = $request->input('column_id');
        $page_size = $request->input('page_size', ENUM_PAGE::PAGE_SIZE);
        $title = $request->input('title');

        $list = $this->article_service->getArticleList($title, $column_id, $page_size);

        return $this->returnMessageService->buildReturnMessage(true, $list, ServerCode::SUCCESS_CODE);
    }


    #[GetMapping(path: "detail/{id}")]
    public function getArticleDetail(int $id)
    {
        $detail = $this->article_service->getArticleDetail($id);
        return $this->returnMessageService->buildReturnMessage(true, $detail, ServerCode::SUCCESS_CODE);
    }
}
